<template>
	<view>
		<view class="center"><text>请假记录(最近开始)</text></view>
		<view>
			<uni-notice-bar show-icon scrollable
							text="被确认的信息无法撤回 !!!" />
			</uni-section>
		</view>
		<view v-for="(item,index) in leave ">
			<uni-card title="请假记录" :extra="item.action">
				<view class="test-component flex-center">
					<hm-news-card>
						<view><text>班级:{{item.request_class_name}}</text></view>
						<view><text>姓名:{{item.request_name}}</text></view>
						<view><text>开始时间:{{item.start_time}}</text></view>
						<view><text>结束时间:{{item.end_time}}</text></view>
						<view><text>原因:{{item.msg}}</text></view>
						<view><text>辅导员:{{item.teacher_name}} 是否确认:{{item.teacher_confirm}}</text></view>
						<view><text>辅导员确认时间:{{item.teacher_confirm_time}}</text></view>
						<view><text>是否返校:{{item.return_confirm}} 确认人:{{item.return_confirm_name}}</text></view>
						<view><text>返校确认时间:{{item.return_confirm_time}}</text></view>
						<view><text>假条创建时间:{{item.CreatedAt|formatTimer}}</text></view>
						<button class="save" @tap="deleteLeave(item.ID)" v-if="!item.teacher_confirm"> 撤 回 </button>
					</hm-news-card>
				</view>
			</uni-card>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				open_id: "",
				page:1,
				size:3,
				leave: [{
					ID:"",
					title:"",
					CreatedAt:"",
					request_id:"",
					request_name:"",
					request_class_id:"",
					request_class_name:"",
					start_time:"",
					end_time:"",
					msg:"",
					teacher_id:"",
					teacher_name:"",
					teacher_confirm:"",
					teacher_confirm_time:"",
					return_confirm:"",
					return_confirm_id:"",
					return_confirm_name:"",
					return_confirm_time:"",
					action:"",
				}],
				search:true,
			}
		},
		methods: {
			getAllLeaveInfo: function() {
				let that = this
				this.$myHttp({
					url: '/leave/student/search ',
					method: "post",
					data: {
						"wx_open_id": this.open_id,
						"page":this.page,
						"size":this.size,
					}
				}).then((res)=>{
					let result = res.data.data
					if (result.length==0){
						uni.showToast({
							title: "没有数据了",
							duration: 3000,
							icon: 'none'
						})
						that.search = false
					}
					that.leave=result
				})
			},
			getAllLeaveInfoDown: function() {
				let that = this
				this.$myHttp({
					url: '/leave/student/search ',
					method: "post",
					data: {
						"wx_open_id": this.open_id,
						"page":this.page,
						"size":this.size,
					}
				}).then((res)=>{
					let result = res.data.data
					if (result.length==0){
						uni.showToast({
							title: "没有数据了",
							duration: 3000,
							icon: 'none'
						})
						that.search = false
					}
					that.leave.push(...result)
				})
			},
			deleteLeave:function(id){
				let that = this
				this.$myHttp({
					url: '/leave/student/delete  ',
					method: "post",
					data: {
						"leave_id": id,
						"wx_open_id":this.open_id,
					}
				}).then((res)=>{
					let result = res.data
					if (result.code == 1000) {
						uni.showToast({
							title: result.msg,
							duration: 1500,
							icon: 'none'
						})
						// 登录成功跳转到个人
						uni.switchTab({
							url: '/pages/home/home'
						})
					}
				})
			}
		},
		  filters: {
		        formatTimer: function(value) {
		          let date = new Date(value);
		          let y = date.getFullYear();
		          let MM = date.getMonth() + 1;
		          MM = MM < 10 ? "0" + MM : MM;
		          let d = date.getDate();
		          d = d < 10 ? "0" + d : d;
		          let h = date.getHours();
		          h = h < 10 ? "0" + h : h;
		          let m = date.getMinutes();
		          m = m < 10 ? "0" + m : m;
		          let s = date.getSeconds();
		          s = s < 10 ? "0" + s : s;
		          return y + "-" + MM + "-" + d + " " + h + ":" + m;
		        },
		      },
		created() {
			this.open_id = uni.getStorageSync('open_id');
			this.getAllLeaveInfo()
		},
		onReachBottom() {
			this.page ++
			if (this.search){
				this.getAllLeaveInfoDown()
			}else{
				uni.showToast({
					title: "没有数据了",
					duration: 3000,
					icon: 'none'
				})
			}
		}
	}
</script>

<style>
	.center {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}
	.save {
		background: #5f8245;
		border: none;
		color: #ffffff;
		margin-top: 40rpx;
		font-size: 28rpx;
	}
</style>